package org.example.TOOL;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.example.util.result.Result;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import static org.example.score.controller.ScoreController.flagdefault;

public class ExcelExporter {

    /**
     * 将 List<Result> 导出为 Excel 文件
     *
     * @param results  数据列表
     * @param filePath 输出文件路径
     */
    public static void exportToExcel(List<Result> results, String filePath) {

        System.out.println("开始导出: " + filePath);

        Path path = null;
        Path path1 = null;
        try {
            //path = Paths.get(System.getProperty("user.dir") + "/src/main/java/org/example/TOOL/Model.xlsx");
            path = Paths.get(System.getProperty("user.dir") + "/src/main/resources/templates/Model.xlsx");
            // path1 = Paths.get(System.getProperty("user.dir") + "/src/main/java/org/example/TOOL/" + filePath + ".xlsx");
            path1 = Paths.get(System.getProperty("user.dir") + "/src/main/resources/files/" + filePath + ".xlsx");
        } catch (Exception e) {
            System.out.println("文件路径错误" + e.getMessage());
            throw new RuntimeException(e);
        }


        try (FileInputStream fileIn = new FileInputStream(path.toFile()); Workbook workbook = new XSSFWorkbook(fileIn)) {

            Sheet sheet = workbook.getSheetAt(0);
            sheet.setRowBreak(4);
            //删除第四行数据

            int oo = 3; // 从第5行开始写入数据
            System.out.println("开始生成");
            if (results == null) {
                System.out.println("数据为空");
            } else {
                System.out.println("开始写入数据");
                for (int pp = 0; pp < results.size(); pp++) {
                    Result res = results.get(pp);
                    Row row = sheet.getRow(oo);
                    oo++;
                    int col = 0;
                    double d = flagdefault;
                    Double def = Double.NaN;
                    row.createCell(col++).setCellValue(pp + 1);
                    row.createCell(col++).setCellValue(res.getXxmc());
                    row.createCell(col++).setCellValue(res.getXnzymc());
                    row.createCell(col++).setCellValue(res.getFzzs()/res.getYxzbs());
                    row.createCell(col++).setCellValue(res.getYxzbs());
                    row.createCell(col++).setCellValue(res.getLqfs() == d ? def : res.getLqfs());
                    row.createCell(col++).setCellValue(res.getZsjhs() == d ? def : res.getZsjhs());
                    row.createCell(col++).setCellValue(res.getDyzyl() == d ? def : res.getDyzyl());
                    row.createCell(col++).setCellValue(res.getBdl() == d ? def : res.getBdl());
                    row.createCell(col++).setCellValue(res.getYsylzy() == d ? def : res.getYsylzy());
                    row.createCell(col++).setCellValue(res.getZzyqk() == d ? def : res.getZzyqk());
                    row.createCell(col++).setCellValue(res.getZymyd() == d ? def : res.getZymyd());
                    row.createCell(col++).setCellValue(res.getGccch() == d ? def : res.getGccch());
                    row.createCell(col++).setCellValue(res.getGcctd() == d ? def : res.getGcctd());
                    row.createCell(col++).setCellValue(res.getSljg() == d ? def : res.getSljg());
                    row.createCell(col++).setCellValue(res.getSsb() == d ? def : res.getSsb());
                    row.createCell(col++).setCellValue(res.getJxsp() == d ? def : res.getJxsp());
                    row.createCell(col++).setCellValue(res.getKysp() == d ? def : res.getKysp());
                    row.createCell(col++).setCellValue(res.getSyjx() == d ? def : res.getSyjx());
                    row.createCell(col++).setCellValue(res.getSxsjsxjdqk() == d ? def : res.getSxsjsxjdqk());
                    row.createCell(col++).setCellValue(res.getYlkcjs() == d ? def : res.getYlkcjs());
                    row.createCell(col++).setCellValue(res.getCbjczl() == d ? def : res.getCbjczl());
                    row.createCell(col++).setCellValue(res.getXxhkcjs() == d ? def : res.getXxhkcjs());
                    row.createCell(col++).setCellValue(res.getGyxljyhdcyqk() == d ? def : res.getGyxljyhdcyqk());
                    row.createCell(col++).setCellValue(res.getXxcx() == d ? def : res.getXxcx());
                    row.createCell(col++).setCellValue(res.getLwcjhgl() == d ? def : res.getLwcjhgl());
                    row.createCell(col++).setCellValue(res.getByzhxlqk() == d ? def : res.getByzhxlqk());
                    row.createCell(col++).setCellValue(res.getJyl() == d ? def : res.getJyl());
                    row.createCell(col++).setCellValue(res.getKyl() == d ? def : res.getKyl());
                    row.createCell(col++).setCellValue(res.getJyqx() == d ? def : res.getJyqx());
                    row.createCell(col++).setCellValue(res.getJyyzyxgd() == d ? def : res.getJyyzyxgd());

                }

            }


            // 写入数据到Excel文件

            try (FileOutputStream fileOut = new FileOutputStream(path1.toFile())) {
                workbook.write(fileOut);
                System.out.println("数据写入成功");
            }

            System.out.println("导出成功");

        } catch (FileNotFoundException e) {
            System.out.println("文件不存在");
            throw new RuntimeException(e);
        } catch (IOException e) {
            System.out.println("文件读取失败");
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
//        // 示例数据
//        List<Result> testData = new ArrayList<>();
//        testData.add(new Result(1, "1001", "School A", "2001", "Major A", 600.0, "3001", "Internal Major A", 600.0, 0.8, 0.9, 0.7, 0.6, 0.85, 10.0, 15.0, 20.0, 4.0, 3.5, 3.8, 4.2, 4.5, 4.0, 3.9, 4.1, 4.3, 4.4, 4.6, 3.5, 3.3, 2.3, 1.5, 6.3, 8.3, 6.3));
//        testData.add(new Result(2, "1002", "School B", "2002", "Major B", 500.0, "3002", "Internal Major B", 500.0, 0.7, 0.8, 0.6, 0.5, 0.75, 9.0, 14.0, 19.0, 3.0, 2.5, 3.2, 3.7, 4.0, 3.8, 4.1, 4.3, 4.5, 4.7, 4.1, 4.3, 4.4, 4.6, 3.5, 3.3, 2.3, 2.3));
//
//
//        // 导出为Excel文件
//        String filePath = "D:/result.xlsx";
//        exportToExcel(testData, filePath);
    }
}
